package com.cyj.dream.admin.persistence.manager.systems;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyj.dream.admin.model.system.SysRoleModule;
import com.cyj.dream.admin.persistence.service.ITbSysRoleModuleService;
import com.cyj.dream.admin.util.IsEmptyUtils;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description: 角色模块关联关系管理器
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.dream.DreamChardonnay.persistence.manager.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-17 10:14
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysRoleModuleManager {

    @Autowired
    private ITbSysRoleModuleService iTbSysRoleModuleService;

    /**
     * 查询角色模块关联关系信息
     *
     * @param sysRoleModule 角色模块关联关系对象
     * @return List<SysRoleModule>
     * @date 2021/5/17 10:15
     * @author ChenYongJia
     * @version 1.0
     */
    public List<SysRoleModule> selectBySysRoleModule(SysRoleModule sysRoleModule) {
        log.info("进入 SysRoleModuleManager 查询角色模块关联关系信息管理器，参数:{}", JSONObject.toJSONString(sysRoleModule));

        LambdaQueryWrapper<SysRoleModule> wrapper = new LambdaQueryWrapper<>();
        if (!IsEmptyUtils.isEmpty(sysRoleModule.getSysModuleId())) {
            wrapper.eq(SysRoleModule::getSysModuleId, sysRoleModule.getSysModuleId());
        }
        if (!IsEmptyUtils.isEmpty(sysRoleModule.getSysRoleId())) {
            wrapper.eq(SysRoleModule::getSysRoleId, sysRoleModule.getSysRoleId());
        }

        return iTbSysRoleModuleService.list(wrapper);
    }

    /**
     * 为角色设置模块
     *
     * @param sysRoleId   角色id
     * @param sysModuleId 模块id集合
     * @return boolean
     * @date 2021/5/17 10:18
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean settingSysRoleModule(Long sysRoleId, Set<Long> sysModuleId) {
        log.info("进入 SysRoleModuleManager 为角色设置模块管理器，角色id：{}，模块id集合:{}", sysRoleId, sysModuleId);
        if (!IsEmptyUtils.isEmpty(sysRoleId) && !IsEmptyUtils.isEmpty(sysModuleId)) {
            List<SysRoleModule> sysRoleModuleList = new ArrayList<>();
            sysModuleId.stream().forEach(item -> {
                SysRoleModule sysRoleModule = new SysRoleModule();
                sysRoleModule.setSysModuleId(item);
                sysRoleModule.setSysRoleId(sysRoleId);
                sysRoleModuleList.add(sysRoleModule);
            });

            try {
                return iTbSysRoleModuleService.saveBatch(sysRoleModuleList);
            } catch (Exception e) {
                log.error("为角色设置模块调用批量添加失败，错误信息为：{}", e);
                return false;
            }
        } else {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

    /**
     * 为角色移除设置的模块
     *
     * @param sysRoleId   角色id
     * @param sysModuleId 模块id集合
     * @return boolean
     * @date 2021/5/17 10:18
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean deleteSysRoleModule(Long sysRoleId, Set<Long> sysModuleId) {
        log.info("进入 SysRoleModuleManager 为角色移除设置的模块管理器，角色id：{}，模块id集合:{}", sysRoleId, sysModuleId);
        if (!IsEmptyUtils.isEmpty(sysRoleId) && !IsEmptyUtils.isEmpty(sysModuleId)) {
            sysModuleId.stream().forEach(item -> {
                LambdaQueryWrapper<SysRoleModule> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysRoleModule::getSysRoleId, sysRoleId);
                wrapper.eq(SysRoleModule::getSysModuleId, item);
                try {
                   iTbSysRoleModuleService.remove(wrapper);
                } catch (Exception e) {
                    log.error("为角色移除设置的模块方法调用删除失败，错误信息为：{}", e);
                }
            });
            return true;
        } else {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

}
